package o2o.task.image;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.ValidateUtil;
import o2o.dao.PO.PojoTables;
import o2o.system.data.SYS_CONFIG_PATH;

import java.io.File;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

//用户提醒
public class ImageTaskUtils {

    // ---------------删除商品中无用的图片
    public static void deleteBadImage() {
        deleteBadGoodsHead();// 删除已删除的商品头像
        deleteBadGoodsImage();// 删除已删除的商品图片
        deleteBadGImage();// 删除已删除的商品对应的图片
    }

    private static void deleteBadGoodsHead() {
        // 查询数据库里的商品对应的图片
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT DISTINCT(imageUrl) url FROM ").append(PojoTables.GoodsInfo).append(" WHERE delFlag = 1");
        List<HashMap<String, Object>> list = null;
        try {
            list = UserDaoSql.getInstance().queryBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return;
        }
        deleteImageFileList(list);

    }


    private static void deleteBadGoodsImage() {
        // 查询数据库里的商品对应的图片
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT url FROM ").append(PojoTables.GoodsImages).append(" WHERE delFlag = 1");
        List<HashMap<String, Object>> list = null;
        try {
            list = UserDaoSql.getInstance().queryBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return;
        }

        deleteImageFileList(list);
    }

    private static void deleteBadGImage() {
        // 查询数据库里的商品对应的图片
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT a.url ")
                .append(" FROM ").append(PojoTables.GoodsImages).append(" a ")
                .append(" LEFT JOIN ").append(PojoTables.GoodsInfo).append(" b ON a.goodsid = b.id ")
                .append(" WHERE b.delFlag = 1");
        List<HashMap<String, Object>> list = null;
        try {
            list = UserDaoSql.getInstance().queryBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return;
        }

        deleteImageFileList(list);
    }


    private static void deleteImageFileList(List<HashMap<String, Object>> list) {
        if (ValidateUtil.isAbsList(list)) {
            for (int i = 0; i < list.size(); i++) {
                if (null == list.get(i)) {
                    continue;
                }
                if (null == list.get(i).get("url")) {
                    continue;
                }
                String url = list.get(i).get("url").toString();
                if (ValidateUtil.isNull(url)) {
                    continue;
                }
                if (ValidateUtil.isNetUrl(url)) {
                    continue;
                }
                String fileName = SYS_CONFIG_PATH.IMAGE_BASE_FILE + url;
                deleteFile(fileName);
            }
        }
    }

    /**
     * 删除单个文件
     *
     * @param fileName 要删除的文件的文件名
     * @return 单个文件删除成功返回true，否则返回false
     */
    private static boolean deleteFile(String fileName) {
        if (ValidateUtil.isNull(fileName)) {
            return false;
        }
        File file = new File(fileName);
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
//                System.out.println("删除单个文件" + fileName + "成功！");
                return true;
            } else {
//                System.out.println("删除单个文件" + fileName + "失败！");
                return false;
            }
        } else {
//            System.out.println("删除单个文件失败：" + fileName + "不存在！");
            return false;
        }
    }


}
